package com.wenhuitec.sysui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wenhuitec.sysui.R;
//import com.wenhui.sysui.R;
import com.wenhuitec.sysui.adapter.AddFoodSkuAdapter;
import com.wenhuitec.sysui.entity.FoodSkuModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class AddFoodSkuActivity extends AppCompatActivity {

    private Context mContext;
    private AddFoodSkuAdapter mAddFoodSkuAdapter;
    private RecyclerView mFoodSkuRv;
    private List<FoodSkuModel> mFoodSkuModelList;
    private Button mSaveBut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_addsku);
        mContext = this;
        mFoodSkuRv = (RecyclerView) findViewById(R.id.foodSkuRv);
        mSaveBut = (Button) findViewById(R.id.saveBut);
        mFoodSkuRv.setLayoutManager(new LinearLayoutManager(mContext));//设置布局管理器


        mFoodSkuModelList = (List<FoodSkuModel>) getIntent().getSerializableExtra("FoodSkuModel");
        if (mFoodSkuModelList == null) {
            mFoodSkuModelList = new ArrayList<>();
            mFoodSkuModelList.add(new FoodSkuModel());
        }
        mAddFoodSkuAdapter = new AddFoodSkuAdapter(mContext, mFoodSkuModelList);
        mFoodSkuRv.setAdapter(mAddFoodSkuAdapter);


        mSaveBut.setOnClickListener(v -> {
            List<FoodSkuModel> foodSkuModelList = mAddFoodSkuAdapter.getmFoodSkuModelList();
            Intent intent = new Intent(mContext, AddFoodSkuActivity.class);
            intent.putExtra("FoodSkuModel", (Serializable) foodSkuModelList);
            setResult(RESULT_OK, intent);
            finish();
        });

    }

    //事件分发方法重写
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //如果是点击事件，获取点击的view，并判断是否要收起键盘
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            //获取目前得到焦点的view
            View v = getCurrentFocus();
            //判断是否要收起并进行处理
            if (isShouldHideKeyboard(v, ev)) {
                hideKeyboard(v.getWindowToken());
                v.clearFocus();
            }
        }
        //这个是activity的事件分发，一定要有，不然就不会有任何的点击事件了
        return super.dispatchTouchEvent(ev);
    }

    //判断是否要收起键盘
    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        //如果目前得到焦点的这个view是editText的话进行判断点击的位置
        if (v instanceof EditText) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            // 点击EditText的事件，忽略它。
            return !(event.getX() > left) || !(event.getX() < right)
                    || !(event.getY() > top) || !(event.getY() < bottom);
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上
        return false;
    }

    //隐藏软键盘并让editText失去焦点
    private void hideKeyboard(IBinder token) {
        if (token != null) {
            //这里先获取InputMethodManager再调用他的方法来关闭软键盘
            //InputMethodManager就是一个管理窗口输入的manager
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            if (im != null) {
                im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    protected void hideSoftKeyboard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0); //强制隐藏键盘
    }

}
